﻿using System.Collections;
using System.IO;
using System.Web;
using System.Web.UI.WebControls;
using ASPNETVN.PORTAL.Components.UI;
using System;
using System.Linq;
using ASPNETVN.PORTAL.Modules.LINK.Components;
using ASPNETVN.PORTAL.Components.Security;

namespace ASPNETVN.PORTAL.Modules.LINK.Controls
{
    public partial class AddEditItem : AddEditItemControl
    {
        public static string[] GetImageList()
        {
            ArrayList ret = new ArrayList();
            string path = HttpContext.Current.Server.MapPath("~/Modules/ASPNETVN.PORTAL.Modules.LINK/Images/Icons");
            if (Directory.Exists(path))
            {
                string[] files = Directory.GetFiles(path);
                foreach (string s in files.Where(x => x.LastIndexOf(".jpg") >= 0 || x.LastIndexOf(".gif") >= 0 || x.LastIndexOf(".png") >= 0 || x.LastIndexOf(".bmp") >= 0))
                {
                    string file = s.Substring(s.LastIndexOf('\\') + 1);
                    ret.Add(file);
                }
            }
            return (string[])ret.ToArray(typeof(string));
        }

        protected override void OnInit(EventArgs e)
        {
            this.Update += AddEditItem_Update;
            this.Delete += AddEditItem_Delete;
            ButtonCancel = this.btnCancel;
            ButtonUpdate = this.btnUpdate;
            ButtonDelete = this.btnDelete;
            base.OnInit(e);
        }

        void AddEditItem_Delete(object sender, EventArgs e)
        {
            if (this.ItemID > 0)
            {
                Item.DeleteItem(this.ItemID);
                this.ReturnToReferrerPage();
            }
        }

        void AddEditItem_Update(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                Item link = Item.Load(this.ItemID) ?? new Item();

                link.ModuleID = this.ModuleID;
                link.Title = txtTitle.Text;
                link.Url = txtUrl.Text;
                link.ImageUrl = ddlImageUrl.SelectedValue;
                link.Description = txtDescription.Text;

                if (txtOrder.Text.Length == 0)
                {
                    link.SortOrder = 0;
                }
                else
                {
                    link.SortOrder = Convert.ToInt32(txtOrder.Text);
                }

                link.Target = cbTarget.SelectedValue;

                if (this.ItemID == 0)
                {
                    link.CreatedBy = PortalSecurity.CurrentUser.Identity.Email;
                    link.CreationDate = DateTime.Now;
                    link.Insert();
                }
                else
                {
                    link.ID = this.ItemID;
                    link.Update();
                }
            }
            this.ReturnToReferrerPage();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack == false)
            {
                // Icons.
                foreach (string s in GetImageList())
                {
                    ddlImageUrl.Items.Add(new ListItem(s, "~/Modules/ASPNETVN.PORTAL.Modules.LINK/Images/Icons/" + s));
                }

                if (this.ItemID > 0)
                {
                    Item link = Item.Load(this.ItemID);
                    if (link != null)
                    {
                        txtTitle.Text = link.Title;
                        txtUrl.Text = link.Url;
                        if (link.ImageUrl.Length > 0)
                        {
                            ddlImageUrl.SelectedValue = link.ImageUrl;
                        }
                        txtDescription.Text = link.Description;
                        txtOrder.Text = link.SortOrder.ToString();
                        cbTarget.SelectedValue = link.Target;
                    }
                }
            }
        }
    }
}